﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Diagnostics;
using System.Text;
using Microsoft.Office.Project.Server.Library;

namespace AddResources
{
    class Program
    {
        static void Main(string[] args)
        {
            String PWAURL=(args[0]);
            Guid secuirtygroupGuid = new Guid(args[1].ToString());
            Guid securityCategoryGuid = new Guid(args[1].ToString());
            Guid sProjectGuid = new Guid(args[2].ToString());

            Guid SECURITY_CATEGORY_OBJECT_TYPE_PROJECT = new Guid("1771B1C0-6E26-4FB3-A480-C798AB506E82");
            WebSvcLoginWindows.LoginWindows loginWindows = new AddResources.WebSvcLoginWindows.LoginWindows();
            WebSvcSecurity.Security security = new AddResources.WebSvcSecurity.Security();


            loginWindows.Url = @PWAURL+"/_vti_bin/PSI/LoginWindows.asmx";
            loginWindows.Credentials = CredentialCache.DefaultCredentials;
            loginWindows.Login();

            security.Url = @PWAURL + "/_vti_bin/PSI/security.asmx";
            security.Credentials = CredentialCache.DefaultCredentials;


            WebSvcSecurity.SecurityCategoriesDataSet dsSecurityCategories
           = new AddResources.WebSvcSecurity.SecurityCategoriesDataSet();

            // Read the existing values for the security category into the dataset
            dsSecurityCategories = security.ReadCategory(securityCategoryGuid);
            Console.WriteLine("No of Projects in the category : " + dsSecurityCategories.SecurityCategoryObjects.Count);

            // Get a new objects row to put the created project into

            WebSvcSecurity.SecurityCategoriesDataSet.SecurityCategoryObjectsRow dsSecurityCategoryObjectsRow
= dsSecurityCategories.SecurityCategoryObjects.NewSecurityCategoryObjectsRow();

            //Set the values
            dsSecurityCategoryObjectsRow.WSEC_OBJ_TYPE_UID = SECURITY_CATEGORY_OBJECT_TYPE_PROJECT;
            dsSecurityCategoryObjectsRow.WSEC_CAT_UID = securityCategoryGuid;
            dsSecurityCategoryObjectsRow.WSEC_OBJ_UID = sProjectGuid;

            // Add the row to the dataset and then pass to SetCategories to update
            dsSecurityCategories.SecurityCategoryObjects.AddSecurityCategoryObjectsRow(dsSecurityCategoryObjectsRow);
            security.SetCategories(dsSecurityCategories);
            Console.WriteLine("Project " + sProjectGuid + " is added into the category now");
            Console.WriteLine("Current Projects in the category :" + dsSecurityCategories.SecurityCategoryObjects.Count);
        }
    }
}